// pages/product-config/product-config.js
Page({
  data: {
    // 学校列表
    schools: [
      { id: 1, name: '北京大学' },
      { id: 2, name: '清华大学' },
      { id: 3, name: '复旦大学' }
    ],
    selectedSchool: {},
    
    // 产品列表
    products: [
      { id: 1, name: '代取快递', type: 'express' },
      { id: 2, name: '食堂代买', type: 'food' },
      { id: 3, name: '洗衣服务', type: 'laundry' }
    ],
    selectedProduct: {},
    
    // 产品配置信息
    productInfo: {
      name: '',
      price: 0,
      type: '',
      notices: [],
      deliveryTimes: [],
      expressPoints: [],
      bargain: {
        enabled: false,
        rules: [
          { people: 20, price: 0 },
          { people: 50, price: 3 },
          { people: 100, price: 6 }
        ]
      },
      couponEnabled: false,
      printerModel: '',
      merchantPhone: ''
    }
  },
  
  // 选择学校
  bindSchoolChange(e) {
    const index = e.detail.value
    this.setData({
      selectedSchool: this.data.schools[index]
    })
    // 加载该学校的产品配置
    this.loadSchoolConfig()
  },
  
  // 添加学校
  addSchool() {
    wx.showModal({
      title: '添加学校',
      content: '请输入学校名称',
      editable: true,
      success: (res) => {
        if (res.confirm && res.content) {
          const newSchool = {
            id: Date.now(),
            name: res.content
          }
          this.setData({
            schools: [...this.data.schools, newSchool],
            selectedSchool: newSchool
          })
          // 初始化新学校的配置
          this.initNewSchoolConfig()
        }
      }
    })
  },
  
  // 选择产品
  bindProductChange(e) {
    const index = e.detail.value
    this.setData({
      selectedProduct: this.data.products[index],
      'productInfo.type': this.data.products[index].type
    })
  },
  
  // 加载学校配置
  loadSchoolConfig() {
    // 这里应该是从服务器获取该学校的配置数据
    // 模拟数据
    const schoolId = this.data.selectedSchool.id
    const mockData = {
      name: '代取快递',
      price: 5,
      type: 'express',
      notices: ['请提供正确的取件码', '贵重物品请当面验收'],
      deliveryTimes: [
        { start: '09:00', end: '11:30' },
        { start: '13:00', end: '17:00' }
      ],
      expressPoints: [
        { name: '菜鸟驿站1号楼', price: 5 },
        { name: '京东快递点', price: 6 }
      ],
      bargain: {
        enabled: true,
        rules: [
          { people: 20, price: 0 },
          { people: 50, price: 3 },
          { people: 100, price: 6 }
        ]
      },
      couponEnabled: false,
      printerModel: 'XP-58',
      merchantPhone: '13800138000'
    }
    
    this.setData({
      productInfo: mockData
    })
  },
  
  // 初始化新学校配置
  initNewSchoolConfig() {
    this.setData({
      productInfo: {
        name: '',
        price: 0,
        type: '',
        notices: [],
        deliveryTimes: [],
        expressPoints: [],
        bargain: {
          enabled: false,
          rules: [
            { people: 20, price: 0 },
            { people: 50, price: 3 },
            { people: 100, price: 6 }
          ]
        },
        couponEnabled: false,
        printerModel: '',
        merchantPhone: ''
      }
    })
  },
  
  // 产品名称输入
  bindProductNameInput(e) {
    this.setData({
      'productInfo.name': e.detail.value
    })
  },
  
  // 价格输入
  bindPriceInput(e) {
    this.setData({
      'productInfo.price': parseFloat(e.detail.value) || 0
    })
  },
  
  // 添加用户须知
  addNotice() {
    this.setData({
      'productInfo.notices': [...this.data.productInfo.notices, '']
    })
  },
  
  // 用户须知输入
  bindNoticeInput(e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    this.setData({
      [`productInfo.notices[${index}]`]: value
    })
  },
  
  // 删除用户须知
  removeNotice(e) {
    const index = e.currentTarget.dataset.index
    const notices = this.data.productInfo.notices.filter((_, i) => i !== index)
    this.setData({
      'productInfo.notices': notices
    })
  },
  
  // 添加配送时间段
  addDeliveryTime() {
    this.setData({
      'productInfo.deliveryTimes': [...this.data.productInfo.deliveryTimes, { start: '', end: '' }]
    })
  },
  
  // 开始时间改变
  bindStartTimeChange(e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    this.setData({
      [`productInfo.deliveryTimes[${index}].start`]: value
    })
  },
  
  // 结束时间改变
  bindEndTimeChange(e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    this.setData({
      [`productInfo.deliveryTimes[${index}].end`]: value
    })
  },
  
  // 删除配送时间段
  removeDeliveryTime(e) {
    const index = e.currentTarget.dataset.index
    const deliveryTimes = this.data.productInfo.deliveryTimes.filter((_, i) => i !== index)
    this.setData({
      'productInfo.deliveryTimes': deliveryTimes
    })
  },
  
  // 添加代取点
  addExpressPoint() {
    this.setData({
      'productInfo.expressPoints': [...this.data.productInfo.expressPoints, { name: '', price: 0 }]
    })
  },
  
  // 代取点名称输入
  bindExpressNameInput(e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    this.setData({
      [`productInfo.expressPoints[${index}].name`]: value
    })
  },
  
  // 代取点价格输入
  bindExpressPriceInput(e) {
    const index = e.currentTarget.dataset.index
    const value = parseFloat(e.detail.value) || 0
    this.setData({
      [`productInfo.expressPoints[${index}].price`]: value
    })
  },
  
  // 删除代取点
  removeExpressPoint(e) {
    const index = e.currentTarget.dataset.index
    const expressPoints = this.data.productInfo.expressPoints.filter((_, i) => i !== index)
    this.setData({
      'productInfo.expressPoints': expressPoints
    })
  },
  
  // 切换砍价活动
  toggleBargain(e) {
    this.setData({
      'productInfo.bargain.enabled': e.detail.value
    })
  },
  
  // 切换优惠券
  toggleCoupon(e) {
    this.setData({
      'productInfo.couponEnabled': e.detail.value
    })
  },
  
  // 打印机型号输入
  bindPrinterModelInput(e) {
    this.setData({
      'productInfo.printerModel': e.detail.value
    })
  },
  
  // 商家手机号输入
  bindMerchantPhoneInput(e) {
    this.setData({
      'productInfo.merchantPhone': e.detail.value
    })
  },
  
  // 保存配置
  saveConfig() {
    if (!this.data.selectedSchool.id) {
      wx.showToast({
        title: '请选择学校',
        icon: 'none'
      })
      return
    }
    
    if (!this.data.productInfo.name) {
      wx.showToast({
        title: '请输入产品名称',
        icon: 'none'
      })
      return
    }
    
    // 这里应该是将配置保存到服务器的代码
    console.log('保存配置:', {
      schoolId: this.data.selectedSchool.id,
      productId: this.data.selectedProduct.id,
      config: this.data.productInfo
    })
    
    wx.showToast({
      title: '保存成功',
      icon: 'success'
    })
  }
})